package com.simple.relaxed.anno.impl;

import com.simple.relaxed.ability.PageBaseAbility;
import com.simple.relaxed.anno.RegisterSlice;
import com.simple.relaxed.manager.impl.SliceManager;
import ohos.aafwk.ability.AbilitySlice;

import java.util.Map;

public class RegisterSliceImpl {

    /**
     * 寻找RegisterSlice注解，寻找SliceManager中是否存在需要注册的AbilitySlice的类对象，若存在，则注册到pageAbility对象中
     *
     * @param pageBaseAbility 要注册AbilitySlice的PageAbility对象
     */
    public static void registerSliceImpl(PageBaseAbility pageBaseAbility) {
        Class<? extends PageBaseAbility> aClass = pageBaseAbility.getClass();
        RegisterSlice registerSlice = aClass.getDeclaredAnnotation(RegisterSlice.class);
        if (registerSlice == null) {
            return;
        }
        String bindName = registerSlice.bindName();
        Map<Class<? extends AbilitySlice>, SliceManager.Entry> map = SliceManager.getInstance().lookup(bindName);
        for (Map.Entry<Class<? extends AbilitySlice>, SliceManager.Entry> entry : map.entrySet()) {
            Class<? extends AbilitySlice> key = entry.getKey();
            SliceManager.Entry value = entry.getValue();
            String route = value.getRoute();
            String name = key.getName();
            pageBaseAbility.addActionRoute(route, name);
        }
    }
}
